package com.ruitu.mall_98_2.mine;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.support.l2_zxing.ZxingUtil;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.Invite;
import com.ruitu.router_module.share.ShareUtil;
import com.ruitu.router_module.share.uitool.ShareBoard;
import com.ruitu.router_module.share.uitool.ShareBoardlistener;
import com.ruitu.router_module.share.uitool.SnsPlatform;

import java.util.HashMap;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class InviteFriendsActivity extends MyBaseActivity {
    private ImageView iv_qr_code;
    private Button btn_invite;
    private TextView tv_invite_code;

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("邀请好友");
        // 不带logo的写法
        // Bitmap mBitmap = ZxingUtil.createImage("你是正确的", 400, 400, null);
        iv_qr_code = findViewById(R.id.iv_qr_code);
        btn_invite = findViewById(R.id.btn_invite);
        tv_invite_code = findViewById(R.id.tv_invite_code);
        btn_invite.setOnClickListener(this);
        tv_invite_code.setOnClickListener(this);
        showProgress();
        getUserInviteCode();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_invite_friends;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (btn_invite ==v){
        if (!isShare){
            showShare();
        }else {
            ToastUtils.showShort("分享已打开");
        }}
        if (tv_invite_code == v){
            copy();
        }
    }

    private void copy(){
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("邀请码", invite.getInviteCode());
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        ToastUtils.showShort("邀请码："+ invite.getInviteCode()+"已复制到剪贴板");
    }

    private Invite invite;
    private void getUserInviteCode() {
        APIRetrofit.getDefault().invitePeople(AppHolder.instance().getUser().getAccountId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult<Invite>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        ToastUtils.showShort("获取邀请码失败");
                    }

                    @Override
                    public void onNext(BaseResult<Invite> inviteBaseResult) {
                        invite = inviteBaseResult.getData();
                        hideProgress();
                        Bitmap mBitmap = ZxingUtil.createImage(inviteBaseResult.getData().getRegistUrl(), 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.icon_logo));
                        iv_qr_code.setImageBitmap(mBitmap);
                        tv_invite_code.setText("邀请码："+inviteBaseResult.getData().getInviteCode());
                    }
                });
    }

    private ShareBoard mShareBoard;//分享面板
    private boolean isShare = false;//是否打开了分享

    private void showShare() {
        if (null == invite || TextUtils.isEmpty(invite.getRegistUrl())) {
            ToastUtils.showShortSafe("获取链接失败");
            return;
        }

        mShareBoard = ShareUtil.getShareBoard(this);
        mShareBoard.setShareboardclickCallback(new ShareBoardlistener() {
            @Override
            public void onclick(SnsPlatform snsPlatform, String platform) {
                hideProgress();
                showProgressWithText(true, "打开分享...");
                isShare = true;
                String shareTxt = invite.getRegistUrl();
//                String shareTitle = System.currentTimeMillis() % 2 == 0 ?
//                        "我在九八商城发现一件不错的商品，快点看看吧！" :
//                        "千挑万选，九八商城这件商品真的物美价廉，快来围观！";
                String shareTitle = "好友邀请";
                shareTxt="邀请码："+invite.getInviteCode();
                ShareParams params = ShareUtil.getShareParams(shareTitle, shareTxt,
                        invite.getRegistUrl());
                Logg.i(params.getUrl());
                JShareInterface.share(platform, params, new PlatActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        hideProgress();
                        ToastUtils.showShortSafe("分享成功");
                        isShare = false;
                    }

                    @Override
                    public void onError(Platform platform, int i, int i1, Throwable throwable) {
                        hideProgress();
                        ToastUtils.showShortSafe("分享错误");
                        isShare = false;
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        hideProgress();
                        ToastUtils.showShortSafe("分享取消");
                        isShare = false;
                    }
                });
            }
        });
        try {
            mShareBoard.show();
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.showShortSafe("发生了一些错误,请稍后重试");
        }
    }
}
